package com.kitemap.widget.business
{
	
	import flash.events.Event;
	import flash.events.EventDispatcher;    
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import com.kitemap.widget.events.AddressEvent;

	public class AddressManager extends EventDispatcher
    {

		private var loginURL:String;
		private var logoutURL:String;
		private var _address:String;
		// TODO: need to refactor this somewhere to comply with DRY - works for now!!
		private var _addressList:Array = ["/","/map","/mousewheel","/profile","/about"];

    	public function AddressManager():void
    	{
    		// nothing
    	}
    	
    	public function init():void
    	{
    		// Listen for SWFAddress updates.
			SWFAddress.addEventListener(SWFAddressEvent.INIT, handleSWFAddressInit);	
		}

		public function handleSWFAddressInit(event:SWFAddressEvent):void
    	{
			trace("handleSWFAddressInit...");
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleSWFAddress);    		
    	}
    	
    	public function handleSWFAddress(event:SWFAddressEvent):void
    	{
    		setAddress(SWFAddress.getValue());
    	}
    	
        public function setAddress(url:String):void 
        {
        	trace("AddressManager::setAddress url = " + url);
        	_address = url;
            dispatchEvent(new Event("addressChange"));
		}
		
		public function setAddressIndex(index:Number):void
		{
			trace("AddressManager::setAddressIndex index = " + index);
			SWFAddress.setValue(_addressList[index]);
		}
        
        [Bindable (event="addressChange")]
        public function get address():String
        {
            return _address;
        }
        
        [Bindable (event="addressChange")]
        public function get selectedIndex():Number
        {
            return _addressList.indexOf(_address);
        }
        
        // TODO: MOVE THESE... 
        public function setLoginUrls(loginURL:String,logoutURL:String):void
        {
        	trace("loginURL:" + loginURL + " logoutURL:" + logoutURL);
        	/*
        	var url = root.loaderInfo.url
			// Is the app running locally?
			isLocal = (url.substr(0, 7) == "file://");
			
			// Save the base url (e.g. http://localhost:8080 or http://myapp.gaespot.com)
			// Notice: There is _no_ trailing space.
			baseURL = url.substr(0, url.indexOf('/', 7));
			*/
        	var baseURL:String = "http://localhost:9000";
        	
			this.loginURL = baseURL + loginURL;
			this.logoutURL = baseURL + logoutURL;
        }

        public function redirectToLogin():void
        {
			if (loginURL) {
				// now redirect to the login url
				trace("getURL" + loginURL);
				var request:URLRequest = new URLRequest(loginURL);
		
				try {
					navigateToURL(request, '_self');
				} catch (e:Error) {
					trace("Error occurred while trying to navigate to " + loginURL);
				}
			}
			else 
			{
				trace("Can't redirect to login until the urls have been set:" + loginURL);
			}
        }

        public function redirectToLogout():void
        {
			if (logoutURL) {
				// now redirect to the logout url
				var request:URLRequest = new URLRequest(logoutURL);
		
				try {
					navigateToURL(request, '_self');
				} catch (e:Error) {
					trace("Error occurred while trying to navigate to " + logoutURL);
				}
			}
			else 
			{
				trace("Can't redirect to logout until the urls have been set:" + logoutURL);
			}
        }
        
        public function getUrls():Object
        {
        	trace("AddressManager::getUrls()");
        	return {
        		'baseUrl':SWFAddress.getBaseURL(),
        		'returnUrl':SWFAddress.getValue()	
        	};
        }
        
    }
}